<include file="Public/common1" />
<block name="css">
    <link rel="stylesheet" type="text/css" href="__CSS__/index/order_detail.css">
    <style>
        .money{ margin-top: 15px; font-size: 16px; font-weight: bloder; }
        .table>tbody>tr>td>input[type="checkbox"]{
            margin: auto;
        }
    </style>
</block>
<block name="content">
    <div class="page-header"><h1>财务管理 &gt;财务结算</h1></div>
    <ul id="nav" class="nav nav-tabs">
      <li role="presentation" <if condition="ACTION_NAME == 'resellerList'">class="active"</if>><a href="{:U('resellerList')}">待审核</a></li>
      <li role="presentation" <if condition="ACTION_NAME == 'balanceList'">class="active"</if>><a href="{:U('balanceList')}">已审核</a></li>
      <li role="presentation" <if condition="ACTION_NAME == 'refuseList'">class="active"</if>><a href="{:U('refuseList')}">审核不通过</a></li>
    </ul>
    <form id="form" action="">
    <!-- 待结算 -->
    <section class="clearfix" id="unSettlement">
        <div class="col-xs-12 clearfix mt-15">
            <div class="col-xs-3">
                <label class="col-xs-4">订单编号</label>
                <div class="col-xs-8"><input type="text" name="order_num" class="form-control" value="<if condition="$_GET['order_num']">{$_GET['order_num']}</if>" /></div>
            </div><div class="col-xs-3">
                <label class="col-xs-4">线路编号</label>
                <div class="col-xs-8"><input type="text" name="line_sn" class="form-control" value="<if condition="$_GET['line_sn']">{$_GET['line_sn']}</if>" /></div>
            </div><div class="col-xs-3">
                <label class="col-xs-4">线路名称</label>
                <div class="col-xs-8"><input type="text" name="line_name" class="form-control" value="<if condition="$_GET['line_name']">{$_GET['line_name'] | urldecode=###}</if>" /></div>
            </div><div class="col-xs-3">
                <label class="col-xs-4">销售人</label>
                <div class="col-xs-8">
                    <select name="reseller_id" class="form-control">
                        <option value="">全部</option>
                        <foreach name="staff" item="v">
                            <option value="{$v['reseller_id']}" <if condition="$_GET['reseller_id'] == $v['reseller_id']">selected</if>>{$v['reseller_name']}</option>
                        </foreach>
                    </select>
                </div>
            </div>
        </div>

        <div class="col-xs-12 clearfix mt-15">
            <div class="col-xs-11">
                <label class="col-xs-1">下单时间</label>
                <div class="col-xs-4"><input id="start" placeholder="开始时间" name="start_time" class="form-control" value="<if condition="$_GET['start_time']">{$_GET['start_time']}</if>"/></div>
                <div class="col-xs-4"><input id="end" placeholder="结束时间" class="form-control"name="end_time" class="form-control" value="<if condition="$_GET['end_time']">{$_GET['end_time']}</if>" /></div>
            </div>

        </div>

        <div class="col-xs-12 clearfix mt-15">
            <div class="col-xs-6">
                <//if condition="ACTION_NAME == 'resellerList'">
                <!-- <button type="button" id="moreBlance" class="btn btn-primary">批量审核</button> -->
                </**/if*/>
                <button type="submit" class="btn btn-primary">查询</button>
                <if condition = "checkAuth('Reseller/Finance/outResellerSettlementListExportExcel')">
                    <button type="button"  id="export" class="btn btn-primary mr-10">导出</button>
                </if>
            </div>
            <div class="col-xs-2 money">成本：<span class="text-danger">&#165;{$orders['info']['sum(need_pay)'] |round=###,3}</span></div>
            <div class="col-xs-2 money">实收：<span class="text-danger">&#165;{$orders['info']['sum(end_need_pay)'] | round=###,2}</span></div>
            <div class="col-xs-2 money">毛利：<span class="text-danger">&#165;{$orders['info']['sum(end_need_pay)'] - $orders['info']['sum(need_pay)'] | round=###,2}</span></div>
        </div>

    </section>

    </form>


    <table id="table" class="table table-striped table-bordered table-hover table-condensed mt-15">
        <thead>
            <tr>
                <th><input type="checkbox" id="checkAll"/></th>
                <th>订单编号</th>
                <th>线路编号</th>
                <th>线路名称</th>
                <!-- <th>订单状态</th> -->
                <th>下单时间</th>
                <th>销售人</th>
                <th>收客人数(成/小)</th>
                <th>成本</th>
                <th>实收</th>
                <th>毛利</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <foreach item="v" name="orders['list']">
                <tr>
                    <td><input type="checkbox" class="checkbox" value="{$v['order_id']}"/></td>
                    <td>{$v['order_num']}</td>
                    <td>{$v['line_sn']}</td>
                    <td>{$v['line_name']}</td>
                    <!-- <td></td> -->
                    <td>{$v['create_time'] | date='Y-m-d', ###}</td>
                    <td>{$v['reseller_name']}</td>
                    <td>{$v['adult_num']}/{$v['child_num']}</td>
                    <td>{$v['need_pay']}</td>
                    <td>{$v['end_need_pay']}</td>
                    <td>{$v['end_need_pay'] - $v['need_pay']} </td>
                    <td><button onclick="openDetail({$v['order_id']})" class="btn btn-primary">详情</button>
                    <!-- <//if condition="ACTION_NAME == 'resellerList'">
                        <button onclick="auditing({$v['order_id']})" class="btn btn-primary">发起审核</button>
                    </*/if*/> -->
                    <if condition="ACTION_NAME == 'refuseList'">
                        <button onclick="cancel({$v['order_id']})" class="btn btn-primary">取消</button>
                    </if>
                    </td>
                </tr>
            </foreach>
        </tbody>
    </table>
    <ul class="pagination pull-right">{$orders['show']}</ul>
</block>
<block name="js">
<script src="__JS__/jedate/jedate.js"></script>
<script>
//全选按钮
$('#checkAll').on('click', function(){
    if($(this).prop('checked')){
        $('#table').find('.checkbox').prop('checked', true);
    } else {
        $('#table').find('.checkbox').prop('checked', false);
    }
})

dateFormat($('#start'), 'YYYY-MM-DD');
dateFormat($('#end'), 'YYYY-MM-DD');

/**
 * 绑定日期选择器
 * @param  {[obj]}    obj        [元素]
 * @param  {[string]} dateFormat [时间格式]
 */
function dateFormat(obj, dateFormat){
    jeDate({
        dateCell: '#'+$(obj).attr('id'),
        format: dateFormat,
        isinitVal:false,
        isTime:true, //isClear:false,
        okfun:function(val){
        }
    });
}

/**
 * 订单取消
 * @param  {[int]}    orderId     [订单id]
 */
function cancel(oId){
    layer.confirm('将此订单放入订单回收站?',
    { btn: ['确认', '取消'] },function(){
        $.post('{:U('SaleManger/cancel')}',{order_id: oId}, function(data){
            layer.msg(data.msg);
            if(data.status == 1){
                setTimeout(function(){
                    location.reload();
                },1000)
                return ;
            }
        },'json')
    })
}

//导出按钮
$('#export').on('click', function(){
    <if condition="ACTION_NAME == 'resellerList'">
        var audit = 1;
    <elseif condition="ACTION_NAME == 'balanceList'"/>
        var audit = 3;
    <else/>
        var audit = -2;
    </if>
    $('#form').attr('action', '/Reseller/Finance/outResellerSettlementListExportExcel/Auditing/'+audit);
    $('#form').submit();
    $('#form').attr('action', '');
})

//批量结算
$('#moreBlance').on('click', function(){
    var $checked = $('#table .checkbox:checked');
    if($checked.length < 1){
        layer.alert('请勾选需要发起审核的订单', {icon: 5})
        return false;
    }
    var oid = [];
    $checked.each(function(i,n){
        oid[i] = $(n).val();
    })
    auditing(oid.join(','));
})

/**
 * 审核请求/单个结算
 * @param string order_id 订单id或订单id字符
 */
function auditing(order_id){
    layer.confirm('发起审核?',{
        btn: ['确认','取消']
    },function(){
        $.post('{:U('auditing')}', {order_id: order_id}, function(data){
            if(data.status == 1){
                layer.msg('审核请求已提交！', {icon: 6, shade: [0.5, '#000']});
                setTimeout(function(){
                    location.reload();
                }, 1500);
            } else {
                 layer.msg('发起审核失败！', {icon: 5, shade:  [0.5, '#000']});
            }
        },'json');
    })

}

/**
 * 退款
 * @param int order_id 订单id
 */
function refuse(order_id){
    layer.confirm('确认退款?',{
        btn: ['确认','取消']
    },function(){
        $.post('{:U('refuse')}', {order_id: order_id}, function(data){
            if(data.status == 1){
                layer.msg('操作成功！', {icon: 6, shade: [0.5, '#000']});
                setTimeout(function(){
                    location.reload();
                }, 1500);
            } else {
                 layer.msg('操作失败', {icon: 5, shade:  [0.5, '#000']});
            }
        },'json');
    })
}

/**
 * 打开订单详情
 * @param int order_id 订单id
 */
function openDetail(order_id){
    layer.open({
        type: 2,
        title: '订单详情',
        shadeClose: true,
        maxmin: true, //开启最大化最小化按钮
        area: ['100%', '100%'],
        content: '/Reseller/Index/orderDetail/order_id/'+order_id
    });
}
</script>
</block>
